package kuaitpai;

import java.util.Random;

public class kp {
    public static void main(String[] args) {
        int[] arr = {1,3,5,8,4,6,9,2,7};
        kp1(arr, 0, arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        mp(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static void mp(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = i+1; j <arr.length ; j++) {
                if (arr[i]> arr[j]){
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }

        }
    }

    public static void kp1(int[] arr, int i, int j) {
        int start = i;
        int end = j;

        int key = arr[i];

        if (start >= end) {
            return;
        }
        while (start != end) {
            while (true) {
                if (arr[end] < key || start >= end) {
                    break;
                }
                end--;
            }
            while (true) {
                if (arr[start] > key || start >= end) {
                    break;
                }
                start++;
            }

            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;


        }
        int temp1 = arr[i];
        arr[i] = arr[start];
        arr[start] = temp1;


        kp1(arr, i, start - 1);
        kp1(arr, start + 1, j);

    }
}
